<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,index) in goodsList" :key='index' @click='gotoDetail(goods)'>
				<my-goods :goods='goods'></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '5',
					// 页码值
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10
				},
				// 商品列表的数据
				goodsList: [],
				// 总数量，用来实现分页
				total: 0,
				isLocading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getqueryObj()
		},
		onReachBottom() {
			const {
				pagenum,
				pagesize
			} = this.queryObj
			if (pagenum * pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
			
			if (!this.isLocading) {
				this.queryObj.pagenum++
				this.getqueryObj()
			}
		},
		onPullDownRefresh() {
			this.goodsList=[]
			this.queryObj.pagenum = 1
			this.total=0
			this.isLocading=false
			this.getqueryObj(() => uni.stopPullDownRefresh())
		},
		methods: {
			gotoDetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods-detail/goods-detail?goods_id=' + goods.goods_id
				})
			},
			async getqueryObj() {
				this.isLocading = true
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				if (meta.status !== 200) return uni.$showMsg()
				this.goodsList = message.goods
				this.total = message.total
				console.log(this.goodsList, this.total)
				this.isLocading = false
			}
		},
	}
</script>

<style lang="scss">

</style>
